package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/23 16:42
 */
public class CustomerProblemQueryFragment extends Fragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.CustomerProblemQueryFragment.EXTRA_CONTENT";
  private final int REQUEST_DATASTATUS = 0;
  private final int REQUEST_REGDATE = 1;
  private final int REQUEST_CUSTOMER = 2;
  private final int REQUEST_CONTRACT = 3;

  @BindView(R.id.tv_titleRight) TextView tvDataStatus;
  @BindView(R.id.ib_dataStatus) ImageView ibDataStatus;
  @BindView(R.id.tv_regDate) TextView tvRegDate;
  @BindView(R.id.ib_regDate) ImageView ibRegDate;
  @BindView(R.id.tv_customerName) TextView tvCustomerName;
  @BindView(R.id.ib_customerName) ImageView ibCustomerName;
  @BindView(R.id.tv_contractName) TextView tvContractName;
  @BindView(R.id.ib_contractName) ImageView ibContractName;
  private String dataStatusCode;
  private String dataStatusName;
  private String regDateCode;
  private String regDateName;
  private String customerName;
  private String contractId;
  private String contractName;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();

    ActionBar actionBar = activity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setTitle(R.string.query);
    }
    ExtraValueContent queryConten =
        (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (queryConten != null) {
      dataStatusName = queryConten.getExtName1();
      dataStatusCode = queryConten.getExtCode1();
      regDateName = queryConten.getExtName2();
      regDateCode = queryConten.getExtCode2();
      customerName = queryConten.getExtName3();
      contractName = queryConten.getExtName4();
      contractId = queryConten.getExtCode4();
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(dataStatusName);
        queryConten.setExtCode1(dataStatusCode);
        queryConten.setExtName2(regDateName);
        queryConten.setExtCode2(regDateCode);
        queryConten.setExtName3(customerName);
        queryConten.setExtName4(contractName);
        queryConten.setExtCode4(contractId);
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_customer_problem_query, container, false);
    ButterKnife.bind(this, view);
    bindData();
    return view;
  }

  private void bindData() {
    tvDataStatus.setText(dataStatusName);
    tvRegDate.setText(regDateName);
    tvCustomerName.setText(customerName);
    tvContractName.setText(contractName);
  }

  @OnClick({ R.id.ib_dataStatus, R.id.ib_regDate, R.id.ib_customerName, R.id.ib_contractName })
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_dataStatus:
        //数据状态
        showDialog(REQUEST_DATASTATUS);
        break;
      case R.id.ib_regDate:
        //登记时间
        showDialog(REQUEST_REGDATE);
        break;
      case R.id.ib_customerName:
        //客户列表
        //                showDialog(REQUEST_CUSTOMER);
        //跳转到客户名录列表界面
        Intent i = new Intent(getActivity(), CustomerListChooseActivity.class);
        startActivityForResult(i, REQUEST_CUSTOMER);
        break;
      case R.id.ib_contractName:
        //合同列表
        showDialog(REQUEST_CONTRACT);
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    FragmentManager fm = getActivity().getSupportFragmentManager();

    switch (requestCode) {
      case REQUEST_DATASTATUS:
        dialog = new ProblemDataStatusDialogFragment();
        break;
      case REQUEST_REGDATE:
        dialog = ProjectPropertyDialogFrament.newInstance(ProjectPropertyDialogFrament.regDate);
        break;
      case REQUEST_CONTRACT:
        dialog = new NewContractDialogFragment();
        break;
      default:
        break;
    }
    assert dialog != null;
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DATASTATUS) {
      CustomerDialog content =
          (CustomerDialog) data.getSerializableExtra(ProblemDataStatusDialogFragment.EXTRA_STATUS);
      dataStatusCode = content.getId();
      dataStatusName = content.getName();
      tvDataStatus.setText(dataStatusName);
    }
    if (requestCode == REQUEST_REGDATE) {
      //登记时间
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      regDateName = customer.getName();
      regDateCode = customer.getId();
      tvRegDate.setText(regDateName);
    }
    if (requestCode == REQUEST_CUSTOMER) {
      //客户
      customerName = data.getStringExtra("customerName");
      String customerId = data.getStringExtra("customerId");
      tvCustomerName.setText(customerName);
    }
    if (requestCode == REQUEST_CONTRACT) {
      //合同
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      contractName = customer.getName();
      contractId = customer.getId();
      tvContractName.setText(contractName);//相关合同
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static CustomerProblemQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    CustomerProblemQueryFragment fragment = new CustomerProblemQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
